
#include <iostream>
#include <mysql.h>
#include<string>
using namespace std;
int main() {
    MYSQL* conn;//创建数据库指针
    MYSQL_RES* result;//指向整张数据表的地址
    MYSQL_ROW row;//数据表行
    conn = mysql_init(NULL);
    if (conn == NULL) {
        std::cout << "mysql_init() failed" << std::endl;
        return 1;
    }
    if (mysql_real_connect(conn, "localhost", "root", "", "", 0, NULL, 0) == NULL) {//进行连接数据库,失败就返回空
        std::cout << "mysql_real_connect() failed: 数据库连接失败" << mysql_error(conn) << std::endl;
        mysql_close(conn);
        return 1;
    }
    //conn->charset("utf8");
    // 执行查询语句
    while (1) {
        cout << "请输入mysql语句-->" << endl;
        string s; getline(cin, s);     
        if (s == "quit"||s=="quit;;")break;
        mysql_query(conn, s.c_str()); 
        cout << "sql命令完成" << endl; 
        //存储查询结果
        result = mysql_store_result(conn);
        if (result) {
            cout << "-----------------------" << endl;
            while (row = mysql_fetch_row(result))
            {
                // cout << "-----------------------" << endl; 
                cout << row[0] << endl;
                //  cout << "-----------------------" << endl;

            }
            cout << "-----------------------" << endl;
        }
    }
    //// 获取查询结果
    //result = mysql_store_result(conn);
    //if (result == NULL) {
    //    std::cout << "mysql_store_result() failed: " << mysql_error(conn) << std::endl;
    //    mysql_close(conn);
    //    return 1;
    //}
    //// 遍历结果集
    //while ((row = mysql_fetch_row(result))) {
    //    std::cout << "ID: " << row[0] << ", Name: " << row[1] << std::endl;
    //}
    //// 释放结果集占用的内存
    //mysql_free_result(result);
    mysql_close(conn);
    return 0;
}
